package virtPresenter.model
{
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.SecurityErrorEvent;
	import flash.events.StatusEvent;
	import flash.net.LocalConnection;
	
	import mx.controls.SWFLoader;
	
	import virtPresenter.event.CairngormErrorEvent;
	
	public class NoProxy implements ISlideWrapperProxy
	{
		private var source:String;
		private var swf:SWFLoader;
		private var processingComplete:Boolean = true;
		private var localConSend:LocalConnection;
		private var localConRec:LocalConnection;
		private var lcConnectionSend:String;
		private var lcConnectionRec:String;
		private var locator:virtPresenterLocator = virtPresenterLocator.getInstance();
		
		private static var LC_ANIM_METHOD:String ="goToAnimStep_lc";
		private static var LC_SLIDE_METHOD:String ="gotoSlide_lc";
		private static var LC_LECTURES_METHOD:String = "setLecturesFolder_lc";
		
		private var requestStack:Array = new Array();
		
		public function NoProxy(source:String, lcConnectionSend:String, lcConnectionRec:String)
		{
			this.source = source;
			this.lcConnectionSend = lcConnectionSend;
			this.lcConnectionRec = lcConnectionRec;
			establishConnection();
		}
		
		private function establishConnection():void{
			swf = new SWFLoader();
			swf.source = this.source;
			
			localConSend = new LocalConnection();
			localConSend.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorEventHandler);
			localConSend.addEventListener(StatusEvent.STATUS, statusEventHandler);
			
			try{
				this.localConRec = new LocalConnection();
				this.localConRec.client = this;
				this.localConRec.connect(this.lcConnectionRec);
			}
			catch(err:Error){
				trace("Flex2SWFConnector. Error in recievingLC!");
				CairngormEventDispatcher.getInstance().dispatchEvent(new CairngormErrorEvent(err,"A: Fehler in recievingLC. Evtl. läuft der virtPresenter bereits in Ihrem Browser. Bitte schließen Sie dann diese Version. Momentan ist es nur erlaubt eine Instanz des virtPresenter zur Zeit laufen zu lassen. Wir bitten um Entschuldigung für jegliche dadurch entstehende Unannehmlichkeiten!"));							
			}
		}
		
		private function securityErrorEventHandler(e:SecurityErrorEvent):void{
			trace("securityErrorEventHandler: " + e);
		}
		
		private function statusEventHandler(e:StatusEvent):void{
			trace("StatusEventHandler: " + e);
		}
		
		public function getSWF():Sprite
		{
			return this.swf;
		}
		
		public function requestSlide(slideNumber:int):void
		{
			localConSend.send(lcConnectionSend, LC_SLIDE_METHOD, Number(slideNumber));
		}
		
		public function requestLecture(lecture:String):void
		{
			localConSend.send(lcConnectionSend, LC_LECTURES_METHOD, lecture);
		}
		
		public function requestAnim(animStepNumber:int):void
		{
			localConSend.send(lcConnectionSend, LC_ANIM_METHOD, Number(animStepNumber));
		}
		
		public function processed(type:String):void
		{
		}
	}
}